package com.wwwarehouse.common.custom_widget.progressbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.ColorInt;
import android.util.AttributeSet;
import android.widget.ProgressBar;

import com.wwwarehouse.common.R;


/**
 * @author : mundane
 * @time : 2017/6/22 10:24
 * @description :
 * @file : HorizontalProgressView.java
 */

public class HorizontalProgressView extends ProgressBar {

	private int mNormalBarColor = getResources().getColor(R.color.common_color_c8_d5d7dc);
	private int mReachBarColor = getResources().getColor(R.color.common_color_c1_337cff);
	private int mTextColor = getResources().getColor(R.color.common_color_c5_45494e);
	private String mTextSuffix = "%";
	//	默认字体大小是12sp
	private int mTextSize = 12;
	private Paint mTextPaint;
	private Paint mNormalPaint;
	private Paint mReachPaint;
	// 默认线的高度是5dp
	private float mLineHeight = 5;
	//	默认线和文字的间距是2dp
	private float mTextOffset = 5;
	// 默认线离左边的距离是34dp
	private float mPaddingLeft = dp2px(34);
	// 默认最右端文字离线的距离是9dp
	private float mTextRightOffset = 9;

	public static final int RIGHT_TOP = 1;
	public static final int RIGHT = 0;
	public static final int FOLLOW = -1;

	private int mProgressPosition = FOLLOW;

	public HorizontalProgressView(Context context) {
		this(context, null);
	}

	public HorizontalProgressView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public HorizontalProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initAttrs(context, attrs);
		initData();
	}

	@Override
	protected synchronized void onDraw(Canvas canvas) {
		canvas.save();
		drawHorizontalProgressView(canvas);
		canvas.restore();
	}

    public void setProgressAndTextColor(@ColorInt int color) {
        mReachPaint.setColor(color);
        mTextPaint.setColor(color);
        invalidate();
    }

	private void drawHorizontalProgressView(Canvas canvas) {
		canvas.translate(0, getHeight() / 2);
		String text = getProgress() + mTextSuffix;
		float textWidth = mTextPaint.measureText(text);
		float textHeight = mTextPaint.descent() + mTextPaint.ascent();
		float totalLineWidth = getMeasuredWidth() - 2 * mPaddingLeft;
		float reachWidth = getProgress() * 1.0f / getMax() * totalLineWidth;
		canvas.drawLine(mPaddingLeft, 0, mPaddingLeft + totalLineWidth, 0, mNormalPaint);
		canvas.drawLine(mPaddingLeft, 0, mPaddingLeft + reachWidth, 0, mReachPaint);

		switch (mProgressPosition) {
			case RIGHT:
				canvas.drawText(text, mPaddingLeft + totalLineWidth + dp2px(mTextRightOffset), 0 - textHeight/2, mTextPaint);
				break;
			case RIGHT_TOP:
				canvas.drawText(text, mPaddingLeft + totalLineWidth - textWidth, 0 - dp2px(mLineHeight / 2 + mTextOffset), mTextPaint);
				break;
			case FOLLOW:
				canvas.drawText(text, mPaddingLeft + reachWidth - textWidth / 2, 0 - dp2px(mLineHeight / 2 + mTextOffset), mTextPaint);
				break;
			default:
				break;
		}

	}

	private void initData() {
		mTextPaint = createTextPaint(mTextColor, sp2px(mTextSize));
		mNormalPaint = createLinePaint(mNormalBarColor, Paint.Style.FILL, dp2px(mLineHeight));
		mReachPaint = createLinePaint(mReachBarColor, Paint.Style.FILL, dp2px(mLineHeight));
	}

	private Paint createLinePaint(int paintColor, Paint.Style style, int width) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
		paint.setColor(paintColor);
		paint.setStyle(style);
		paint.setStrokeWidth(width);
		return paint;
	}

	private Paint createTextPaint(int paintColor, int textSize) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
		paint.setColor(paintColor);
		paint.setTextSize(textSize);
		return paint;
	}

	private void initAttrs(Context context, AttributeSet attrs) {
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HorizontalProgressView);
		mProgressPosition = a.getInt(R.styleable.HorizontalProgressView_progressTextPosition, FOLLOW);
		a.recycle();
	}

	public int px2dp(float paValue) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (paValue / scale + 0.5f);
	}

	public int dp2px(float dpValue) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	public int px2sp(float pxValue) {
		final float frontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
		return (int) (pxValue / frontScale + 0.5f);
	}

	public int sp2px(float spValue) {
		final float frontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
		return (int) (spValue * frontScale + 0.5f);
	}
}
